<template>
  <div class="product-con">
    <van-nav-bar title="标题" fixed>
      <template #left>
        <img src="imgs/icon-left.png" alt="" @click="$router.back()">
      </template>
      <template #title>
        <van-tabs v-model="active" @click="onClick">
          <van-tab title="商品"></van-tab>
          <van-tab title="详情"></van-tab>
          <van-tab title="推荐"></van-tab>
        </van-tabs>
      </template>
    </van-nav-bar>
    <div class="w">
      <!-- 商品信息 -->
      <div class="product" ref="product">
        <div class="swiper">
          <van-swipe class="my-swipe" :autoplay="5000" indicator-color="white" @change="onChange">
            <van-swipe-item v-for="(item,index) in fouseImgs" :key="index"><img :src="item" alt=""></van-swipe-item>
            <template #indicator>
              <div class="custom-indicator">{{ current + 1 }}/{{fouseImgs.length}}</div>
            </template>
          </van-swipe>
        </div>
        <div class="product-msg">
          <h2 class="price">￥<span>{{maskEdition.price}}</span></h2>
          <h3 class="name">{{productList.name}}</h3>
          <div class="config">
            <p v-for="(item,index) in productList.text" :key="index"><span>{{index+1}}</span>{{item}}</p>
          </div>
          <div class="config-swiper" @click="show=true">
            <van-swipe :loop="false" :width="84">
              <van-swipe-item v-for="(item,index) in productConfig" :key="index" style="width:2.24rem;">
                <img :src="item.imgUrl" alt="">
                <p>{{item.name}}</p>
                <p class="text">{{item.text}}</p>
              </van-swipe-item>
              <template #indicator>
                <i></i>
              </template>
            </van-swipe>
          </div>
          <van-popup v-model="show" round position="bottom" :style="{ height: '75%' }" closeable>
            <div class="mask-box">
              <h2>关键参数</h2>
              <div class="mask-item clearfix" v-for="(item,index) in maskConfig " :key="index">
                <div class="item-left">{{item.left}}</div>
                <div class="item-right">{{item.right}}</div>
              </div>

              <van-button color="linear-gradient(to right, #ff6034, #ee0a24)" size="large" round @click="show= false">
                确定
              </van-button>
            </div>
          </van-popup>
        </div>
      </div>
      <!-- 商品选择 -->
      <div class="buy">
        <div class="productType" @click="showTwo">
          <div class="selectType">已选</div>
          <div class="type">{{maskName}}x1 <img src="imgs/icon-right.png" alt=""> </div>
        </div>
        <van-popup v-model="showBuy" round position="bottom" :style="{ height: '80%' }" closeable>
          <div class="mask-box ">
            <div class="box-top clearfix">
              <div class="img">
                <img :src="maskColor.imgUrl" alt="">
              </div>
              <div class="text">
                <p class="price">￥{{maskEdition.price}}</p>
                <p class="name">{{maskName}}</p>
              </div>
            </div>
            <!-- 商品版本选择 -->
            <div class="edition clearfix">
              <h3>版本</h3>
              <div class="item" :class="{action:item.seleted}" v-for="(item,index) in maskList.edition" :key="index" @click="changeAction(maskList.edition,index)">
                {{item.text}}
              </div>
            </div>
            <div class="color clearfix">
              <h3>颜色</h3>
              <div class="item" :class="{action:item.seleted}" v-for="(item,index) in maskList.color" :key="index" @click="changeAction(maskList.color,index)">
                {{item.color}}
              </div>
            </div>
            <div class="tc clearfix">
              <h3>套餐</h3>
              <div class="item action">
                标配
              </div>
            </div>
            <!-- 购买数量 -->
            <div class="buyNum clearfix">
              <h3>购买数量 </h3>
              <div class="num">
                <van-icon name="minus" @click="addOrBuy(1)" /><span>{{num}}</span>
                <van-icon name="plus" @click="addOrBuy(2)" />
              </div>
            </div>
            <div class="button-box" v-show="showButton === 1">
              <div class="left" @click="addShopCartorBuy(1)">加入购物车</div>
              <div class="right" @click="addShopCartorBuy(2)">立即购买</div>
            </div>
            <van-button color="linear-gradient(to right, #fdcf00, #fd9b00)" size="large" round @click="addShopCartorBuy(1)" v-show="showButton ===2">
              添加到购物车
            </van-button>
            <van-button color="linear-gradient(to right, #ff6034, #ee0a24)" size="large" round @click="addShopCartorBuy(2)" v-show="showButton ===3">
              立即购买
            </van-button>
          </div>
        </van-popup>
      </div>
      <div class="section">
        <div class="section-box clearfix">
          <div class="section-item"><img src="imgs/icon-succeed.png" alt="">小米直营</div>
          <div class="section-item"><img src="imgs/icon-succeed.png" alt="">小米发货</div>
          <div class="section-item"><img src="imgs/icon-succeed.png" alt="">15天无理由退货</div>
        </div>
        <div class="img">
          <img src="imgs/icon-right.png" alt="">
        </div>
      </div>
      <div class="line"></div>
      <!-- 商品参数 -->
      <div class="part" ref="container">
        <van-sticky :container="container">
          <div class="sticky-box">
            <a href="javascript:;" @click="showBox(1)" :class="{'action':showBoxNum==1}">商品介绍</a>
            <a href="javascript:;" @click="showBox(2)" :class="{'action':showBoxNum==2}">配置参数</a>
          </div>
        </van-sticky>
        <!-- 商品信息的图片区 -->
        <div class="part-one-imgs" v-if="showBoxNum==1">
          <img v-for="(item,index) in partOne" :key="index" v-lazy="item" alt="">
        </div>
        <div class="part-two-imgs" v-else>
          <img v-for="(item,index) in partTwo" :key="index" v-lazy="item" alt="">
        </div>

      </div>
      <div class="line"></div>
      <!-- 推荐 -->
      <div class="recommed" ref="recommed">
        <h2>商品推荐</h2>
        <ul>
          <li class="recommed-item" v-for="(item,index) in recommed" :key="index">
            <img v-lazy="item.imgUrl" alt="">
            <div class="text">
              <h3 class="name">{{item.name}} </h3>
              <p class="price">￥{{item.price}}</p>
            </div>
          </li>

        </ul>
      </div>
    </div>
    <!-- 底部按钮 -->
    <div class="footer">
      <router-link to="/">
        <van-icon name="home-o" size="0.56rem" />
        <p>首页</p>
      </router-link>
      <a type="javascript:;">
        <van-icon name="service-o" size="0.56rem" />
        <p>客服</p>
      </a>
      <router-link to="/shopcart">
        <van-icon name="cart-o" size="0.56rem" />
        <p>购物车</p>
        <NumBer :numBer="$store.getters.sum" v-show="$store.getters.seletedProduct.length>=1"></NumBer>
      </router-link>
      <div class="botton">
        <div class="left" @click="showAddCart">
          加入购物车
        </div>
        <div class="right" @click="showGoBuy">
          立即购买
        </div>
      </div>
    </div>
    <!-- 加载添加商品 -->
    <van-overlay :show="addShow" @click="addShow = false">
      <div class="wrapper" @click.stop>
        <van-loading size="40px" vertical color="#fff" v-if="addOrSucceed">添加到购物车中...</van-loading>
        <div class="succeed-box" v-else>
          <van-icon name="success" color="#fff" size="1rem" />
          <p>添加成功</p>
        </div>
      </div>
    </van-overlay>
  </div>
</template>

<script>
import { goBuy } from '@/api/requests.js'
import { set } from '@/Storage/index.js'
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: 'Product',
  data() {
    return {
      active: 0,
      current: 0,
      showBoxNum: 1,
      container: null,
      // 轮播图图片数据
      fouseImgs: [],
      // 推荐商品数据
      recommed: [],
      // 商品参数
      partOne: [],
      partTwo: [],
      // 商品配置
      productConfig: [],
      // 商品信息
      productList: {},
      productOffSetTop: 0,
      partOffSetTop: 0,
      recommedOffSetTop: 0,
      // 控制弹窗是否显示
      show: false,
      // 弹窗商品配置
      maskConfig: [],
      showBuy: false,
      maskList: {},
      // 判断显示那个种按钮
      showButton: 1,
      // 商品添加获取购买数量
      num: 1,
      // 显示添加商品
      addShow: false,
      // 控制现在添加中或者添加成功
      addOrSucceed: true
    }
  },
  mounted() {
    this.container = this.$refs.container
    this.getproductList()
    this.getOffsetTop()
    window.addEventListener('scroll', this.getScrollTop)
  },
  methods: {
    onClick(index) {
      if (index === 0) {
        window.scrollTo(0, this.productOffSetTop)
      } else if (index === 1) {
        window.scrollTo(0, this.partOffSetTop)
      } else {
        window.scrollTo(0, this.recommedOffSetTop)
      }
    },
    onChange(index) {
      this.current = index
    },
    // 展示商品信息或者展示商品参数
    showBox(num) {
      this.showBoxNum = num
    },
    async getproductList() {
      const res = await this.$API.productList(this.$route.params.id)
      this.fouseImgs = res.data.fouseImgs
      this.recommed = res.recommed
      this.partOne = res.partOne
      this.partTwo = res.partTwo
      this.productConfig = res.productConfig
      this.productList = res.data.productList
      this.maskConfig = res.maskConfig
      this.maskList = res.maskList
    },
    // 获取三个分类的盒子距离头部的位置
    getOffsetTop() {
      // 利用nextTic
      this.$nextTick(() => {
        setTimeout(() => {
          this.productOffSetTop = this.$refs.product.offsetTop
          this.partOffSetTop = this.$refs.container.offsetTop
          this.recommedOffSetTop = this.$refs.recommed.offsetTop
        }, 1000)
      })
    },
    // 获取滚动卷走的距离
    getScrollTop() {
      let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      scrollTop = Math.ceil(scrollTop)
      if (scrollTop >= this.partOffSetTop && scrollTop < this.recommedOffSetTop) {
        this.active = 1
      } else if (scrollTop >= this.recommedOffSetTop) {
        this.active = 2
      } else {
        this.active = 0
      }
    },
    // 修改商品的选中
    changeAction(arr, i) {
      arr.forEach(item => {
        item.seleted = false
      })
      arr[i].seleted = true
    },
    // 显示添加到购物车
    showAddCart() {
      this.showBuy = true
      this.showButton = 2
    },
    // 显示立即购买
    showGoBuy() {
      this.showBuy = true
      this.showButton = 3
    },
    // 显示添加购物车和立即购买
    showTwo() {
      this.showBuy = true
      this.showButton = 1
    },
    // 修改商品数量
    addOrBuy(type) {
      if (type === 1) {
        if (this.num <= 1) {
          return false
        } else {
          this.num--
        }
      } else {
        if (this.num >= 10) {
          return false
        } else {
          this.num++
        }
      }
    },
    // 添加商品到购物车或者购买商品
    async addShopCartorBuy(type) {
      // 将商品的信息存储到一个对象中发起请求再作为返回值添加到购物车里面
      // 购物车中需要 id name num 型号 价格
      const obj = {}
      Object.assign(obj, this.maskEdition, this.maskColor)
      obj.name = this.maskName
      obj.num = this.num
      // 区分是添加商品还是购买商品
      if (type === 1) {
        this.addShow = true
        this.addOrSucceed = true
        obj.status = false
        setTimeout(() => {
          this.addOrSucceed = false
          this.$store.dispatch('addShopCart', obj)
          setTimeout(() => {
            this.addShow = false
          }, 1300)
        }, 2000)
      } else {
        const res = await goBuy()
        if (res) {
          set('productList', obj)
          this.$store.commit('CHANGESHOWORDER', true)
          this.$router.push('/setOrder')
        } else {
          this.$notify({ type: 'primary', message: '先登入' })
        }
      }
      this.showBuy = false
    }
  },
  computed: {
    maskName() {
      return `${this.maskList.name} ${this.maskEdition.text} ${this.maskColor.color}`
    },
    maskEdition() {
      return this.seletedEdition.find(item => item.seleted) || {}
    },
    maskColor() {
      return this.seletedColor.find(item => item.seleted) || {}
    },
    seletedEdition() {
      return this.maskList.edition || []
    },
    seletedColor() {
      return this.maskList.color || []
    }
  },
  destroyed() {
    window.removeEventListener('scroll', this.getScrollTop)
  }
}
</script>

<style lang='less' scoped>
.product-con {
  .van-nav-bar--fixed {
    padding: 0 30px;
  }
  .w {
    width: 720px;
    margin: 0 auto;
    font-size: 28px;
    padding-bottom: 90px;
    .mask-box {
      .box-top {
        width: 656px;
        height: 219px;
        padding: 30px 0 25px 0;
        .img {
          float: left;
          position: relative;
          width: 167px;
          height: 167px;
          background: #f5f5f5;
          border-radius: 8px;
          img {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            width: 141px;
            height: 141px;
          }
        }
        .text {
          float: left;
          margin-left: 30px;
          .price {
            font-size: 32px;
            color: #ff6700;
          }
        }
      }
      .item {
        float: left;
        padding: 30px 20px;
        background: #f5f5f5;
        line-height: 0;
        border-radius: 30px;
        margin: 0 20px 30px 0;
        border: 1px solid #fff;
      }
      .action {
        background: #fff2ef;
        color: #ff6700;
        border: 1px solid #ff6700;
      }
      .button-box {
        display: flex;
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translate(-50%, 0);
        width: 658px;
        height: 72px;
        overflow: hidden;
        border-radius: 40px;
        color: #fff;
        text-align: center;
        line-height: 72px;
        div {
          width: 50%;
          height: 100%;
        }
        .left {
          background-image: linear-gradient(90deg, #fdcf00, #fd9b00);
        }
        .right {
          background-image: linear-gradient(90deg, #ff7310, #fe3f00);
        }
      }
      .buyNum {
        h3 {
          float: left;
        }
        .num {
          float: right;
          span {
            display: inline-block;
            width: 60px;
            height: 60px;
            line-height: 60px;
            text-align: center;
            background-color: #f9f9f9;
            margin: 0 15px;
            vertical-align: middle;
            font-size: 40px;
          }
        }
      }
    }
  }
  .product {
    margin-bottom: 20px;
    .swiper {
      img {
        width: 720px;
        height: 792px;
      }
      .custom-indicator {
        position: absolute;
        right: 5px;
        bottom: 10px;
        padding: 2px 5px;
        font-size: 12px;
        width: 100px;
        height: 45px;
        background: rgba(0, 0, 0, 0.5);
        color: #fff;
        font-size: 20px;
        line-height: 45px;
        text-align: center;
        border-radius: 10px;
      }
    }
    .product-msg {
      padding: 22px 26px;
      background: #f6f6f6;
      border-radius: 15px;
      .price {
        color: #ff6700;
        span {
          font-size: 48px;
        }
      }
      .name {
        margin: 20px 0;
        font-size: 32px;
      }
      .config {
        padding: 0 30px;
        font-size: 24px;
        border-bottom: 1px solid #666;
        p {
          position: relative;
          margin-bottom: 15px;
          span {
            position: absolute;
            top: 5px;
            left: -25px;
            width: 24px;
            height: 24px;
            text-align: center;
            border: 1px solid #aeaeae;
            line-height: 20px;
            border-radius: 12px;
            font-size: 18px;
            color: #aeaeae;
          }
        }
      }
      .config-swiper {
        margin-top: 20px;
        .van-swipe-item {
          text-align: center;
          img {
            width: 30px;
            height: 30px;
          }
          .text {
            white-space: nowrap;
            overflow: hidden;
            text-overflow: ellipsis;
          }
        }
      }
    }
  }
  .buy {
    height: 88.8px;
    line-height: 88.8px;
    .productType {
      display: flex;
      justify-content: space-between;
      padding: 0 0 0 32px;
      border-bottom: 1px solid #dbdbdb;
      .type {
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: 598px;
        padding-right: 25px;
        img {
          width: 36px;
          height: 36px;
        }
      }
    }
  }
  .section {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 72px;
    background-color: #fcfcfc;
    padding: 0 32px;
    .section-item {
      float: left;
      margin-right: 20px;
      img {
        width: 24px;
        height: 24px;
        vertical-align: middle;
        margin-right: 8px;
      }
    }
    .img {
      img {
        width: 36px;
        height: 36px;
      }
    }
  }
  .part {
    margin-bottom: 30px;
    img {
      vertical-align: top;
    }
    .sticky-box {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 68px;
      text-align: center;
      background: #fff;
      a {
        display: inline-block;
        width: 360px;
      }
    }
    .action {
      color: #ff6700;
    }
  }
  .recommed {
    padding: 90px 0 0 0;
    h2 {
      padding: 0 32px;
      font-size: 28px;
      height: 80px;
      line-height: 80px;
    }
    ul {
      display: flex;
      flex-wrap: wrap;
      li {
        width: 50%;
        img {
          width: 356px;
          height: 356px;
        }
        .text {
          font-size: 26px;
          padding: 0 32px;
          .price {
            color: #ff6700;
            margin-top: 15px;
          }
        }
      }
    }
  }
  .footer {
    display: flex;
    align-items: center;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 90px;
    padding: 0 20px;
    text-align: center;
    background-color: #fff;
    font-size: 24px;
    a {
      position: relative;
      width: 84px;
      padding: 0 5px;
    }
    .botton {
      display: flex;
      width: 444px;
      height: 72px;
      overflow: hidden;
      border-radius: 40px;
      margin-left: 15px;
      color: #fff;
      line-height: 72px;
      div {
        width: 222px;
        height: 100%;
      }
      .left {
        background-image: linear-gradient(90deg, #fdcf00, #fd9b00);
      }
      .right {
        background-image: linear-gradient(90deg, #ff7310, #fe3f00);
      }
    }
  }
  .mask-box {
    position: relative;
    font-size: 28px;
    padding: 0 32px 66px;
    h2 {
      text-align: center;
      margin: 25px 0 22px;
    }
    button {
      position: absolute;
      bottom: 0;
      left: 50%;
      transform: translate(-50%, 0);
      width: 658px;
      height: 72px;
      font-size: 34px;
    }
    .mask-item {
      padding-bottom: 30px;
      div {
        float: left;
      }
      .item-left {
        width: 150px;
        margin-right: 30px;
      }
    }
  }
  .wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    .succeed-box {
      color: #fff;
      text-align: center;
    }
    .van-overlay {
      z-index: 999;
    }
  }
}
</style>
<style scoped lang='less'>
/deep/ .van-nav-bar__content {
  height: 90px;
}
/deep/ .van-tab {
  font-size: 24px;
}
/deep/ .van-tab__text--ellipsis {
  overflow: visible;
}
/deep/ .van-tabs__nav {
  width: 300px;
}
/deep/ .van-nav-bar__left {
  img {
    width: 50px;
    height: 50px;
  }
}
/deep/ .van-sticky--fixed {
  top: 90px;
}
</style>
